package com.hz.ruoyistorage.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hz.ruoyistorage.domain.LgColdStorage;
import com.hz.ruoyistorage.domain.LgColdType;
import com.hz.ruoyistorage.domain.LgGoods;
import com.hz.ruoyistorage.domain.LgSubCold;
import com.hz.ruoyistorage.domain.mypojo.ColdType2;
import com.hz.ruoyistorage.mapper.LgColdStorageMapper;
import com.hz.ruoyistorage.mapper.LgColdTypeMapper;
import com.hz.ruoyistorage.mapper.LgGoodsMapper;
import com.hz.ruoyistorage.mapper.LgSubColdMapper;
import com.hz.ruoyistorage.service.LgColdStorageService;
import com.hz.ruoyistorage.service.LgSubColdService;
import com.hz.ruoyistorage.service.LgSubColdSomeService;
import com.hz.ruoyistorage.service.LgUnitService;
import com.hz.ruoyistorage.utils.ResultJSON;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.LinkedList;
import java.util.List;


/**
 * @version 1.0
 * @Author gj
 * @Date 2025/3/8 9:28
 * @注释
 */
//给余震五的api
@RestController
@RequestMapping("/api")
public class SubColdSomeApi {
    @Autowired
    private LgSubColdService lgSubColdService;
    @Autowired
    private LgSubColdSomeService lgSubColdSomeService;
    @Autowired
    private LgUnitService lgUnitService;
    @Autowired
    private LgColdTypeMapper lgColdTypeMapper;
    @Autowired
    private LgGoodsMapper lgGoodsMapper;
    @Autowired
    private LgColdStorageService lgColdStorageService;
    @Autowired
    private LgColdStorageMapper lgColdStorageMapper;
    @Autowired
    private LgSubColdMapper lgSubColdMapper;


    @PostMapping("/getCreateBy")
    public String getCreateBy(@RequestParam("subId") Integer subId)
    {
        LgSubCold lgSubCold = lgSubColdMapper.selectById(subId);
        String createBy = lgSubCold.getCreateBy();
        return createBy;
    }


    @RequestMapping("/getUnitAll")
    public ResultJSON getAll() {
        return ResultJSON.success(lgUnitService.list());
    }
    @RequiresPermissions("storage:smallCold:list")
    @GetMapping("/getColdSome")
    public ResultJSON getColdSome()
    {
        return ResultJSON.success(lgSubColdService.getSubColdSome());
    }

    @GetMapping("/getColdType")
    public ResultJSON getColdType()
    {
        List<LgColdType> list = lgColdTypeMapper.getColdType();
        List<ColdType2> list2 = new LinkedList<>();
        for (LgColdType lgColdType : list) {
            ColdType2 coldType2 = new ColdType2(lgColdType.getColdTypeId(), lgColdType.getTypeName());
            list2.add(coldType2);
        }
        return ResultJSON.success(list2);
    }
    //获得大类信息

    @GetMapping("/getParent")
    public ResultJSON getParent()
    {
        QueryWrapper<LgGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("parent_id");

        List<LgGoods> list = lgGoodsMapper.selectList(queryWrapper);
        return ResultJSON.success(list);
    }
    //获得子类信息
    @GetMapping(value = "/goodsId")
    public ResultJSON getChild(@RequestParam("goodsId") Integer goodsId)
    {
        QueryWrapper<LgGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",goodsId);
        List<LgGoods> list = lgGoodsMapper.selectList(queryWrapper);
        return ResultJSON.success(list);
    }

    //返回大冷库id,name,容积
    @GetMapping("/getColdCount")
    public List getColdCount()
    {
        QueryWrapper<LgColdStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("cold_id","cold_name","remaining_volume");
        return lgColdStorageService.list(queryWrapper);
    }

    //根据小冷库id,改变冷库的容积和状态
    @PostMapping("/updateCold")
    public void updateCold(@RequestParam("subId") Integer subId, @RequestParam("yunsuan")String yunsuan)
    {
        //获得小冷库容积
        LgSubCold lgSubCold = lgSubColdService.selectById(subId);
        Double capacity = lgSubCold.getCapacity();
        //获得大冷库容积
        Integer parentId = lgSubColdService.selectById(subId).getParentId();
        LgColdStorage lgColdStorage = lgColdStorageService.getById(parentId);
        Double remainingVolume = lgColdStorage.getRemainingVolume();
        //进行运算
        if(yunsuan.equals("+")){
            lgColdStorage.setRemainingVolume(remainingVolume+capacity);
            //修改小冷库状态为1
            lgSubColdMapper.updateStatus(subId,1);
        }
        if(yunsuan.equals("-")){
            if(remainingVolume-capacity<0){
                return;
            }
            lgColdStorage.setRemainingVolume(remainingVolume-capacity);
            //修改小冷库状态为2
            lgSubColdMapper.updateStatus(subId,2);
        }
        //修改容积
        lgColdStorageMapper.updateCold(lgColdStorage.getRemainingVolume(),lgColdStorage.getColdId());
    }

    @GetMapping("/getColdAdress")
    public List getColdAdress()
    {
        QueryWrapper<LgColdStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("cold_name","coordinate_x","coordinate_y");
        return lgColdStorageService.list(queryWrapper);
    }



}
